<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Table\AgentUserSelectTable;
use App\Admin\Extensions\Table\UserSelectTable;
use App\Admin\Repositories\AgentUser;
use App\Models\Area;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;

class AgentController extends AdminController
{

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AgentUser(['user']), function (Grid $grid)  {
            $grid->column('id','代理商ID')->sortable();
            $grid->column('user.nickname','用户昵称');
            $grid->column('name');
            $grid->column('phone','手机号');
            $grid->column('username','代理商账号');
            $grid->column('avatar','代理商头像');
//            $grid->column('address','详细地址')->limit(15);
//            $grid->column('shop_good_count','商品数量');
            $grid->column('status')->using(['正常','隐藏'],true)->label(['info','default']);
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand(false);
                $filter->equal('id')->width(3);
                $filter->like('user.nickname','用户昵称')->width(3);
                $filter->like('shop_user.username','代理商账号')->width(3);
                $filter->like('name')->width(3);
                $filter->like('phone','手机号')->width(3);
//                $filter->like('address','详细地址')->width(3);
                $filter->equal("status","状态")->select(['正常','隐藏'])->width(3);
            });
//            $grid->actions(function (Grid\Displayers\Actions $actions){
//                $id = $actions->getKey();
//                if ($id==1){
//                    $actions->disableDelete();
//                }
//            });
//            $grid->rowSelector()->disable(function ($row) {
//                return $row->id === 1;
//            });
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AgentUser(), function (Form $form) {
            $form->column(5,function (Form $form){
                $form->selectTable('user_id','所属用户')->title('选择用户')
                    ->dialogWidth('60%') // 弹窗宽度，默认 800px
                    ->from(UserSelectTable::make(['id' => $form->getKey(),'is_limit'=>true, 'table' =>'agent_users'])) // 设置渲染类实例，并传递自定义参数
                    ->model(User::class, 'id', 'nickname'); // 设置编辑数据显示
                $form->text('name')->required();
                $form->text('phone','手机号')->required();
                $form->title("账号信息");
                $id = $form->getKey();
                $message = ['unique' => '此账号已存在' ];
                $rules = [
                    'required'
                ];
                if($id){
                    $rules[] = Rule::unique('agent_users','username')->ignore($id);
                }else{
                    $rules[] = Rule::unique('agent_users','username');
                }
                $form->text('username','账号')->rules($rules,$message)->required();
                $id = $form->getKey();
                $password = $form->password('password','密码')
                    ->rules(['between:6,15','alpha_num'],['between'=>'请输入6-15为字符','alpha_num'=>'密码必须为大小写字母或者数字'])
                    ->minLength(6,'最少6位字符')
                    ->maxLength(15,'最长15位字符')
                    ->attribute("autocomplete","new-password");
                if($id){
                    $password->help("不修改密码,请留空");
                }else{
                    $password->required();
                }
//                $form->selectTable('pid','上级代理')->title('选择上级代理')
//                    ->dialogWidth('60%') // 弹窗宽度，默认 800px
//                    ->from(AgentUserSelectTable::make()) // 设置渲染类实例，并传递自定义参数
//                    ->model(\App\Models\AgentUser::class, 'id', 'name');
                $form->radio('status')->options(['正常','隐藏'])->default(0);
            });
            $form->column(7,function (Form $form){
                $form->image('avatar','后台头像')->autoUpload()->help("图片尺寸要求:100*100");
                $form->rate("agent_rate","门店每笔交易-代理商分配比例")->width(3)->default(0);
                $form->rate("top_rate","门店每笔交易-上级用户分配比例")->width(3)->default(0);
                $form->rate("top_top_rate","门店每笔交易-上上级用户分配比例")->width(3)->default(0);
            });

            $form->saving(function (Form $form){
//                $form->pid = $form->pid?:0;
                $form->user_id = $form->user_id?:0;
                if($form->isCreating()){
                    $form->password = Hash::make($form->password);
                }
                if ($form->isEditing()){
                    if ($form->password){
                        $form->password = Hash::make($form->password);
                    }else{
                        $form->password = $form->model()->password;
                    }
                }
            });
        });
    }
}
